<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */


namespace App\Api\Service\Order\Controllers;

use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Order;
use App\Eloquent\Ygt\OrderProcess;
use App\Eloquent\Ygt\Process;
use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\OrderProcessCourse;
use App\Eloquent\Ygt\OrderProcessCourseLog;
use App\Engine\Func;
use DB;

class CourseController extends Controller
{
    //工序列表
    public function processLists()
    {
        $userId                 = Func::getHeaderValueByName( 'userid',1 );
        $user                   = DepartmentUser::getCurrentInfo($userId);
        if(!$user)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $user->company_id;
        $where                  = ['is_last'=>1,'company_id'=>$companyId];
        $fields                 = 'id,title';
        $colletion              = Process::getList($where,$fields);
        $result                 = $colletion->toArray();
        return $result;
    }
    //工序的订单列表
    public function courseList()
    {
        $userId                 = Func::getHeaderValueByName( 'userid',1 );
        $processId              = request('process_id',0);
        $user                   = DepartmentUser::getCurrentInfo($userId);
        if(!$user || !$processId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where                  = ['id'=>$processId];
        $process                = Process::getInfo($where)->toArray();

        $page                   = request( 'page', 1 );
        $limit                  = request('limit', 10 );
        $offset                 = ( $page - 1 ) * $limit;

        //待封装至一个方法
        $companyId              = $user->company_id;
        $orderProcessObj        = DB::table('ygt_order_process as a');
        $select                 = ['a.order_id','b.id','b.order_process_id','b.uid','b.status'];
        $orderProcessObj        = $orderProcessObj->select($select);
        $joinTable              = 'ygt_order_process_course as b';
        $joinleft               = 'b.order_process_id';
        $joinRight              = 'a.id';
        $orderProcessObj        = $orderProcessObj->leftJoin($joinTable, $joinleft, '=', $joinRight);
        $orderProcessObj        = $orderProcessObj->where('a.company_id',$companyId);
        $orderProcessObj        = $orderProcessObj->where('a.process_type',$processId);
        $orderProcessObj        = $orderProcessObj->where('b.status',1);
        $count                  = $orderProcessObj->count();
        $orderProcessObj        = $orderProcessObj->offset($offset)->limit($limit)->orderby('b.id','desc');
        $orderProcessCollection = $orderProcessObj->get();
        $lists                  = $orderProcessCollection->toArray();
        //过滤组合数据
        $uidArr                 = $orderIdArr = [];
        foreach($lists as $key=>$val)
        {
            $orderIdArr[]       = $val->order_id;
            $uidArr[]           = $val->uid;
        }
        $where                  = ['company_id'=>$companyId,'user_id'=>['in',$uidArr]];
        $departmentUserArr      = DepartmentUser::getList($where)->pluck('truename','user_id')->all();
        $select                 = ['id','order_title'];
        $orderObj               = Order::select($select);
        $orderObj               = $orderObj->whereIn('id',$orderIdArr);
        $orderCollection        = $orderObj->get();
        $orderArr               = $orderCollection->pluck('order_title','id')->all();
        foreach($lists as $key=>$val)
        {
            $tempUid                         = $val->uid;
            $tempOrderId                     = $val->order_id;
            $lists[$key]->truename          = isset($departmentUserArr[$tempUid]) ? $departmentUserArr[$tempUid] : '已被删除';
            $lists[$key]->order_title       = isset($orderArr[$tempOrderId]) ? $orderArr[$tempOrderId] : '脏数据0.0';
            $lists[$key]->process_name      = $process['title'];
            $lists[$key]->readme             = '其中 id 是传值时需要的 order_process_course_id';
        }
        foreach($lists as $key=>$val)
        {
//              //若和朱的页面保持统一则调此处
//              //保持一致则参考getOrderDetail和confirmCompleteBaseInfo方法中$inputType注释的地方
////            $url        = '/api/service/order/getOrderDetail?type=1&user_id='.$val->uid.'&order_process_course_id='.$val->id;
////            echo '朱详情:<a href="'.$url.'" target="_blank">'.$val->truename.'-'.$val->uid.'-'.$val->process_name.'</a>';
////            $url        = '/api/service/order/confirmCompleteBaseInfo?type=1&order_process_course_id='.$val->id;
////            echo '<br/>';
////            echo '朱完工:<a href="'.$url.'" target="_blank">'.$val->truename.'-'.$val->uid.'-'.$val->process_name.'</a>';
////            echo '<hr/>';
//              //若是另起新页面则调此处
            $url        = '/api/service/order/courseOrderDetail?type=1&course_id='.$val->id;
            echo '接单详情:<a href="'.$url.'" target="_blank">'.$val->truename.'-'.$val->uid.'-'.$val->process_name.'</a>';
            $url        = '/api/service/order/courseOrderDetail?type=2&course_id='.$val->id;
            echo '<br/>';
            echo '完工详情:<a href="'.$url.'" target="_blank">'.$val->truename.'-'.$val->uid.'-'.$val->process_name.'</a>';
            echo '<hr/>';
        }exit;
        $pagingData             = [
            'page'=>$page, 'pageSize'=>$limit,'total'=>$count,'keyword' => ''
        ];
        return $this->pagingData($lists, $pagingData);
    }
    //工序的订单详情-若和朱保持统一则忽略
    public function courseDetail()
    {
        $type                   = request('type',0);
        $courseId               = request('course_id',0);
        switch($type)
        {
            case 1:
                //接单详情
                $where                  = ['id'=>$courseId];
                $courseColletion        = OrderProcessCourse::getInfo($where);
                $orderProcessId         = $courseColletion->order_process_id;
                /***********开始**********/
                //待朱提供的方法直接调用用来和已有的页面数据保持一致[ape]
                //链接可以[参考]courseList中注释掉的代码块
                $orderProcessCollection = OrderProcess::where('id',$orderProcessId)->first();
                $orderProcessInfo       = $orderProcessCollection->toArray();
                /***********结束**********/
                $result                 = ['course_id'=>$courseId,'order_process'=>$orderProcessInfo];
                break;
            case 2:
                //确认完工详情
                /***********开始**********/
                //待朱提供的方法直接调用用来和已有的页面数据保持一致
                //链接可以[参考]courseList中注释掉的代码块
                $where                  = ['id'=>$courseId];
                $courseColletion        = OrderProcessCourse::getInfo($where);
                $result                 = $courseColletion->toArray();
                /***********结束**********/
                break;
            default:
                xThrow( ERR_UNKNOWN );
        }
        return $result;
    }
    //工序的订单详情提交-若和朱保持统一则忽略
    public function courseDetailSubmit()
    {
        $type                   = request('type',0);
        $courseId               = request('course_id',0);
        switch($type)
        {
            case 1:
                //接单详情提交
                $where                  = ['id'=>$courseId];
                $data                   = ['status'=>2];
                $updateResult           = OrderProcessCourse::updateOneData($where,$data);
                break;
            case 2:
                //确认完工详情提交
                $where                  = ['id'=>$courseId];
                $data                   = ['status'=>4];
                $updateResult           = OrderProcessCourse::updateOneData($where,$data);
                break;
            default:
                $updateResult           = false;
        }
        if(!$updateResult)
        {
            xThrow( ERR_UNKNOWN );
        }
        $result                 = ['message'=>'成功'];
        return $result;
    }
}